import { reqLogin } from "../../request/api"
import { Toast } from 'antd-mobile'

const initState = {
    user: {
        phone: "10086",
        password: "123"
    },
    userInfo: JSON.parse(sessionStorage.getItem("userInfo"))?JSON.parse(sessionStorage.getItem("userInfo")):{}
}
/* 
nickname: "123"
phone: "10086"
token: "eyJzZWMiOiJSU0EiLCJ0eXBlIjoiSldUIn0=.               eyJuYmYiOjE2MjI4NzYxODg0NzIsInVpZCI6ImJlMTIzZjMwLWM0NDAtMTFlYi05MzE1LTI5OTQ0ZDM2NjlhNCIsImxhc3RUaW1lIjozNjAwMDAwfQ==.aAgLJNqhND8/8RkwuKv/DYchATAbLhBPPaUUOn8aHL79FMCHBWWugGHudArAeNZqcepYYOnSKGyI/IhDR1i+aLYl9+VsyeKAiv04WSDB5wW4pemcMC4fvr3Occi4P5RBQiVQ61CJvSdkP8xvMB0YqF+rViDoyQfQYtWioEpVCS19jsh7VxPowZBvnNYNhh9qrE/eQvgtXN19m/BkI1YEyJ9i1WQlkR/TDfWNFluxyqbRWZQ6AfvdoDWXNXbIiGin2W3fg8ECSopNmrzCcybT8cVOCz7YT5e4ocYDDHsarLP3umMecphNRnvt+0ocBiZANHUNC2QE4oCJq0CqxHCnAw=="
uid: "be123f30-c440-11eb-9315-29944d3669a4" */
const TYPES = {
    CHANGE_LOGIN_USER: "CHANGE_LOGIN_USER",
    CHANGE_LOGIN_USERINFO:"CHANGE_LOGIN_USERINFO"
}

export const actions = {
    change_login_user: (value, key) => ({ type: TYPES.CHANGE_LOGIN_USER, value, key }),
    change_login_userInfo: (obj) => ({ type: TYPES.CHANGE_LOGIN_USERINFO, obj }),
    reqLogin: (router) => {
        return (dispatch, getState) => {
            let { user } = getState().login
            reqLogin(user).then(res => {
                if (res.code === 200) {
                    Toast.info(res.msg)
                    dispatch(actions.change_login_userInfo(res.list))
                    sessionStorage.setItem('userInfo',JSON.stringify(res.list))
                    router.push("/index/mall")
                }
            })
        }
    }
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.CHANGE_LOGIN_USER:
            return {
                ...state,
                user: {
                    ...state.user,
                    [action.key]: action.value
                }
            }
        case TYPES.CHANGE_LOGIN_USERINFO:
            return {
                ...state,
                userInfo: action.obj
            }
        default:
            return state;
    }
}
export default reducer

export let get_login_user = state => state.login.user

